<template>
  <div class="container">
    <div class="header">
      <label class="switch">
        <input type="checkbox" v-model="info.enabled" />
        <span class="slider round"></span>
      </label>
      <div class="input-group">
        <label class="input-label">{{ $i('rpc.form.name') }}</label>
        <input v-model="info.name" placeholder="$i('rpc.form.placeholder.name')" class="input-field" />
      </div>
      <div class="input-group">
        <label class="input-label">RPC服务方法</label>
        <input v-model="info.path" placeholder="RPC服务方法" class="input-field" />
      </div>
      <button @click="addRow" class="add-row-button">新增行</button>
    </div>
    <div class="table-container">
      <table class="editable-table">
        <thead>
        <tr>
          <th>字段名称</th>
          <th>测试值</th>
          <th>参数类型</th>
          <th>描述</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(row, index) in info.paramData" :key="index">
          <td><input v-model="row.fieldName" class="table-input" /></td>
          <td><input v-model="row.testValue" class="table-input" /></td>
          <td>
            <select v-model="row.paramType" class="table-select">
              <option value="数值">数值</option>
              <option value="字符串">字符串</option>
              <option value="集合">集合</option>
              <option value="Map">Map</option>
              <option value="object">object</option>
            </select>
          </td>
          <td><input v-model="row.description" class="table-input" /></td>
          <td><button @click="removeRow(index)" class="action-button">删除</button></td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup>
import { inject } from 'vue'

const $i = inject('i18n.format')
const info = inject('info')
//console.log(info);
const addRow = () => {
  if (!Array.isArray(info.value.paramData)) {
    info.value.paramData = [];
  }
  info.value.paramData.push({ fieldName: '', testValue: '', paramType: '数值', description: '' });
};

const removeRow = (index) => {
  if (info.value.paramData.length >= 1) {
    info.value.paramData.splice(index, 1);
  }
};
</script>

<style scoped>
.container {
  width: 100%;
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-left: 10px;
  margin-bottom: 10px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 30px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:checked + .slider:before {
  transform: translateX(22px);
}

.input-group {
  display: flex;
  align-items: center;
  margin-left: 10px;
}

.input-label {
  margin-right: 5px;
  font-weight: bold;
  color: #333;
  font-size: 12px;
}

.input-field {
  padding: 6px 10px;
  font-size: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 150px;
}

.table-container {
  margin-top: 10px;
  max-height: 150px; /* 设置最大高度 */
  overflow-y: auto; /* 添加垂直滚动条 */
  border: 1px solid #ddd; /* 添加边框以区分表格 */
  position: relative; /* 相对定位 */
}

.editable-table {
  width: 100%;
  border-collapse: collapse;
  border: none; /* 去掉表格边框 */
  font-size: 12px;
}

.editable-table th {
  background-color: #f8f8f8;
  color: #333;
  padding: 6px;
  text-align: left;
  border-bottom: 1px solid #ddd;
  border-right: 1px solid #ddd;
  position: sticky; /* 固定表头 */
  top: 0;
  z-index: 1; /* 确保表头在上方 */
}

.editable-table th:last-child {
  border-right: none;
}

.editable-table td {
  padding: 0;
  border-bottom: 1px solid #ddd;
  border-right: 1px solid #ddd;
}

.editable-table td:last-child {
  border-right: none;
}

.table-input, .table-select {
  width: 100%;
  height: 100%;
  padding: 6px;
  font-size: 12px;
  border: none;
  box-sizing: border-box;
}

.table-select {
  appearance: none; /* 去掉默认的下拉箭头 */
  -webkit-appearance: none;
  -moz-appearance: none;
  background: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3E%3Cpath fill='%23333' d='M2 0L0 2h4zm0 5L0 3h4z'/%3E%3C/svg%3E") no-repeat right 8px center;
  background-size: 10px 10px;
}

.action-button {
  width: 100%;
  height: 100%;
  padding: 6px;
  background-color: #ff4d4d;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 12px;
  box-sizing: border-box;
}

.action-button:hover {
  background-color: #e64a4a;
}

.add-row-button {
  margin-left: 20px;
  padding: 6px 12px;
  background-color: #2196F3;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 12px;
  box-sizing: border-box;
}

.add-row-button:hover {
  background-color: #0b7dda;
}
</style>




